/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.acl.server.dao;

import java.util.List;

import nl.biz_e.acl.shared.model.Authorization;
import nl.biz_e.acl.shared.model.Context;
import nl.biz_e.acl.shared.model.User;
import nl.biz_e.acl.shared.model.UserGroup;
import nl.biz_e.appengine.lib.server.objectify.AbstractBaseDao;
import nl.biz_e.appengine.lib.server.objectify.ObjectifyUtils;

import com.googlecode.objectify.ObjectifyService;

/**
 * DAO for accessing and manipulating {@link Authorization} entities
 * 
 * @author Joost A. Bloemsma
 * 
 */
public class AuthorizationDao extends AbstractBaseDao<Authorization> {
    static {
        ObjectifyService.register(Authorization.class);
    }

    /**
     * Constructs a new AuthorizationDao instance
     */
    public AuthorizationDao() {
        super(Authorization.class);
    }

    /**
     * Returns the authorization for the passed user/cpntext combination
     * 
     * @param user
     * @param ctx
     * @return instance of {@link Authorization}
     */
    public Authorization get(User user, Context ctx) {
        return ObjectifyUtils.getResultSingle(ofy().query(Authorization.class)
                .filter("user", user).filter("context", ctx));
    }

    /**
     * @param group
     * @param ctx
     * @return instance of {@link Authorization}
     */
    public Authorization get(UserGroup group, Context ctx) {
        return ObjectifyUtils.getResultSingle(ofy().query(Authorization.class)
                .filter("group", group).filter("context", ctx));
    }

    /**
     * @param user
     * @return list of {@link Authorization} instances
     */
    public List<Authorization> getAuthorizations(User user) {
        return ofy().query(Authorization.class).filter("user", user).list();
    }

    /**
     * @param group
     * @return list of {@link Authorization} instances
     */
    public List<Authorization> getAuthorizations(UserGroup group) {
        return ofy().query(Authorization.class).filter("group", group).list();
    }

    /**
     * @param ctx
     * @return list of {@link Authorization} instances
     */
    public List<Authorization> getAuthorizations(Context ctx) {
        return ofy().query(Authorization.class).filter("context", ctx).list();
    }
}
